package cn.com.huzo.common.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.springframework.util.ResourceUtils;

/**
 * 读取应用程序配置
 *
 * @author 吴旭
 */
public class ApplicationConfiguration {

    private static PropertiesConfiguration config;

    static {
        try {
            File cfgFile = ResourceUtils.getFile("classpath:huzo-erp.properties");
            FileBasedConfigurationBuilder<PropertiesConfiguration> builder
                    = new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
                    .configure(new Parameters().properties()
                            .setFile(cfgFile)
                            .setThrowExceptionOnMissing(true)
                            .setListDelimiterHandler(new DefaultListDelimiterHandler(';'))
                            .setIncludesAllowed(false));
            config = builder.getConfiguration();
        } catch (FileNotFoundException | ConfigurationException ex) {
            Logger.getLogger(ApplicationConfiguration.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * 读取应用程序名
     * <p>
     * @return
     */
    public static String getAppName() {
        if (config != null) {
            return config.getString("ApplicationName");
        } else {
            return "";
        }
    }

    /**
     * 读取公司名称
     * <p>
     * @return
     */
    public static String getCompanyName() {
        if (config != null) {
            return config.getString("CompanyName");
        } else {
            return "";
        }
    }

    /**
     * 读取属性
     * <p>
     * @param key 属性标识
     * @return
     */
    public static Object getProperty(String key) {
        if (config != null) {
            return config.getProperty(key);
        } else {
            return "";
        }
    }
}
